package com.common.util.a4yy.impl;

import com.a4yy.PlayerList;
import com.a4yy.ReloveListBean;
import com.common.util.a4yy.IRelove;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class ReloveImpl implements IRelove {
    @Override
    public List<ReloveListBean> getList(String content) {
        return null;
    }

    @Override
    public List getSearch(String content) {
        Document document = Jsoup.parse(content);
        //搜索列表
        //获取上一页和下一页链接

        Elements elements = document.select("#long-page>a.pagelink_a");
        List list = new ArrayList();
        if(!elements.isEmpty()){
            String prev = null;
            String next=null;
            for(Element e:elements){
                if("上一页".equals(e.text())){
                    prev="http://aaxxy.com"+e.attr("href");
                }else if("下一页".equals(e.text())){
                    next="http://aaxxy.com"+e.attr("href");
                }
            }
            list.add(prev);
            list.add(next);
        }
        elements = document.select("#contents>li");
        if(!elements.isEmpty()){
            for(int i=0;i<elements.size();i++){
                Element e = elements.get(i);
                //链接地址
                Element url = e.select("a.play-img").get(0);
                //图片
                Element img=e.select("a.play-img>img").get(0);
                //标题
                Elements ts = e.select("div.play-txt>h5>a>font");
                if(ts.isEmpty()){
                    ts = e.select("div.play-txt>h3>a");
                }
                Element title=ts.get(0);
                ReloveListBean reloveListBean = new ReloveListBean();
                reloveListBean.setName(title.text());
                if(img.attr("data-original")!=null && img.attr("data-original").length()>0){
                    reloveListBean.setImageUrl(img.attr("data-original"));
                }else{
                    reloveListBean.setImageUrl("http:"+img.attr("src"));
                }
                reloveListBean.setDetailUrl("http://aaxxy.com"+url.attr("href"));
                list.add(reloveListBean);
            }
        }
        return list;
    }

    @Override
    public ReloveListBean getDetail(String content) {
        Document document = Jsoup.parse(content);
        //电影标题
        Elements elements=document.select("#detail-box>div.detail-title.fn-clear>h1");
        String name=null;
        if(!elements.isEmpty()){
            name=elements.get(0).text();
        }
        //截取电影截图
        elements = document.select("#detail-box>div.detail-cols.fn-clear>div.detail-pic.fn-left>img");
        String imgUrl=null;
        if(!elements.isEmpty()){
            imgUrl="http:"+elements.get(0).attr("src");
        }
        //获取电影描述
        String description=null;
        elements = document.select("#juqing>div.tjuqing");
        if(!elements.isEmpty()){
            description=elements.get(0).text();
        }
        //获取集数
        elements = document.select("#detail-list>div.play-list-box");
        ReloveListBean reloveListBean = new ReloveListBean();
        reloveListBean.setDescription(description);
        reloveListBean.setName(name);
        if(!elements.isEmpty()){
            List<PlayerList> playerLists = new ArrayList<>(5);
            for(int i=0;i<elements.size();i++){
                PlayerList playList = new PlayerList();
                Element e=elements.get(i);
                Elements et = e.select("div.caption.fn-clear>h4>a>img");
                if(!et.isEmpty()){
                    String title=et.get(0).attr("alt");
                    playList.setPlayerArea(title);
                }
                Elements as=e.select("div.content>p.play-list>a");
                List<PlayerList.PlayerLinkDetail> list = new ArrayList<>(10);
                if(!as.isEmpty()){
                    for(int j=0;j<as.size();j++){
                        Element v = as.get(j);
                        PlayerList.PlayerLinkDetail linkDetail = new PlayerList.PlayerLinkDetail();
                        linkDetail.setName(v.text());
                        linkDetail.setUrl(v.attr("href"));
                        list.add(linkDetail);
                    }
                }
                playList.setList(list);
                playerLists.add(playList);
            }
            reloveListBean.setPlayerList(playerLists);
        }
        reloveListBean.setImageUrl(imgUrl);
        return reloveListBean;
    }

    @Override
    public void openView(String url) throws Exception{
        Desktop.getDesktop().browse(new URI("http://aaxxy.com"+url));
    }
}
